var server = require('./utils/server');
var md5 = require('./utils/md5.js');

// 授权登录 
App({
  onLaunch: function() {
    // auto login via SDK
    var that = this;
    //AV.User.loginWithWeapp();
    // 设备信息
    wx.getSystemInfo({
      success: function(res) {
        that.screenWidth = res.windowWidth;
        that.pixelRatio = res.pixelRatio;
      }
    });
  },
  //当前位置信息
  location:function(){
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        // console.log(latitude)
        getApp().loadCity(longitude, latitude)
      }
    })
  },
  loadCity: function (longitude, latitude) {
    // var page = this
    wx.request({
      url: 'https://api.map.baidu.com/geocoder/v2/?ak=NEbTPzAWGyHz9USX5wXiDRhiMrboReD9&location=' + latitude + ',' + longitude + '&output=json',
      data: {},
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        // console.log(res)
        let city = res.data.result.addressComponent.city + '|' + res.data.result.addressComponent.district
        getApp().globalData.adds = city;
      },
      fail: function () {
      },

    })
  },
  getOpenId: function(cb) {
    wx.login({
      success: function(res) {
        if (res.code) {
          server.getJSON("/User/getOpenid", {
            url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wx9d52fed4fef55edf&secret=7ff926f7ad74985c3e3ff77f3169b901&js_code=' + res.code + '&grant_type=authorization_code&code=' + res.code
          }, function(res) {
            // 获取openId
            var openId = res.data.openid;
            // TODO 缓存 openId
            var app = getApp();
            var that = app;
            that.globalData.openid = openId;
            //验证是否关联openid
            typeof cb == "function" && cb()
          });
          //发起网络请求
        }
      }
    });


  },

  register: function(cb) {
    var app = this;
    this.getUserInfo(function() {
      var openId = app.globalData.openid;
      var userInfo = app.globalData.userInfo;
      var country = userInfo.country;
      var city = userInfo.city;
      var gender = userInfo.gender;
      var nick_name = userInfo.nickName;
      var province = userInfo.province;
      var avatarUrl = userInfo.avatarUrl;


      server.getJSON('/User/register?open_id=' + openId + "&country=" + country + "&gender=" + gender + "&nick_name=" + nick_name + "&province=" + province + "&city=" + city + "&head_pic=" + avatarUrl, function(res) {
        app.globalData.userInfo = res.data.res
        console.log(456786);
        console.log(res.data.res);
        typeof cb == "function" && cb()
      });

    })
  },
  getUserInfo: function(cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function() {
          wx.getUserInfo({
            success: function(res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  globalData: {
    'openid': null,
    'userInfo': null,
    'login': false,
    'globalUrl': 'https://shops.dasouwang.cn/index.php/',
    "username":'',
    "token":'',
    'adds': '' , //当前位置信息 => 郑州市|中原区
    'lng':'',
    'lat':'',
    'city':'淮阳县',
    'site_id':411626,
  }
})